package com.hongxun.design.mytools;

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;

import com.alibaba.fastjson.JSON;

import com.hongxun.design.MyApp;

/**
 * Created by zerdoor_pc on 2016/5/5.
 */
public class GetPhoneStrengh {

    /* 我们可以用它们onResume和onPause方法停止listene */
    TelephonyManager Tel;
    MyPhoneStateListener MyListener;
    Context context;
    static  GetPhoneStrengh intence;

    GetPhoneStrengh(Context context) {
        this.context = context;
        MyListener = new MyPhoneStateListener();
        Tel = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    }

    public static GetPhoneStrengh getItence(Context context) {
        if (intence == null) {
            intence = new GetPhoneStrengh(context);
        }
        return intence;
    }

    /* Called when the application is minimized */

    public void onPause() {

        Tel.listen(MyListener, PhoneStateListener.LISTEN_NONE);
    }

    /* Called when the application resumes */

    public void onResume() {

        Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    }

    /* 开始PhoneState听众 */
    public class MyPhoneStateListener extends PhoneStateListener {
        /* 从得到的信号强度,每个tiome供应商有更新 */
        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            super.onSignalStrengthsChanged(signalStrength);
            MyLog.L(MyApp.AppName + "MyPhoneStateListener:" + JSON.toJSONString(signalStrength));
//ToastShow.showShort(context, "Go to Firstdroid!!! GSM Cinr = " + String.valueOf(signalStrength.getGsmSignalStrength()));

        }
    }

}
